/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageImport;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.utils.Debug;



/**
 * 
 */
public class ChooseImportElementDialog extends Dialog{

	/**
	 * 
	 */
	protected Combo elementcombo;
	
	/**
	 * 
	 */
	protected Combo modelcombo;
	
	/**
	 * 
	 */
	protected ArrayList<Package> packageList;
	
	/**
	 * 
	 */
	protected List elementmodelList;
	
	/**
	 * 
	 */
	protected String[] elementList= {	"       "};
	
	/**
	 * 
	 */
	protected Text nameField;
	
	/**
	 * 
	 */
	protected String announceModel="Choose your package";
	
	/**
	 * 
	 */
	protected String announceElement="Choose your element";
	
	/**
	 * 
	 */
	protected int channel=19;
	
	/**
	 * 
	 */
	protected Object value=null;
	
	/**
	 * 
	 */
	protected ModelManager modelManager;
	
	/**
	 * 
	 */
	protected Package currentPackage;

	/**
	 * 
	 * 
	 * @param parentShell 
	 * @param modelManager 
	 * @param currentPackage 
	 */
	public ChooseImportElementDialog(Shell parentShell,Package currentPackage, ModelManager modelManager){
		super(parentShell);
		this.modelManager=modelManager;
		this.currentPackage=currentPackage;
		Debug.debug(this,"ChooseDialog", channel);
	}


	/**
	 * 
	 */
	protected class ModelComboListener implements SelectionListener{
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetDefaultSelected(SelectionEvent e){	

		}
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetSelected(SelectionEvent e){runActionModel();}
	};
	
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	protected Control createDialogArea(Composite parent){
		Composite comp=(Composite)super.createDialogArea(parent);

		GridLayout layout=(GridLayout)comp.getLayout();
		layout.numColumns=2;


		//line model
		packageList =new ArrayList<Package>();
		packageList.add(modelManager.getTopPackageFromUMLResource());
		Iterator it=modelManager.getTopPackageFromUMLResource().getMembers().iterator();
		while(it.hasNext()){
			Element e= (Element)it.next();
			if (e instanceof Package){
				packageList.add((Package)e);}
		}


		Label modellabel =new Label(comp, SWT.RIGHT|SWT.SINGLE);
		modellabel.setText(announceModel);
		modelcombo = new Combo (comp, SWT.READ_ONLY);
		elementList=new String[packageList.size()];
		for (int i=0;i<packageList.size();i++){
			elementList[i]="   "+packageList.get(i).getQualifiedName()+"   ";
		}


		modelcombo.setItems (elementList);
		modelcombo.setSize (400, 400);
		modelcombo.addSelectionListener(new ModelComboListener());

		//line Element
		Label elementlabel =new Label(comp, SWT.RIGHT|SWT.SINGLE);
		elementlabel.setText(announceElement);
		elementList= new String[1];
		elementList[0]="                                               "+
		"                                               ";
		elementcombo = new Combo (comp, SWT.READ_ONLY);
		elementcombo.setItems (elementList);
		elementcombo.setSize (400, 400);
		Debug.debug(this,"createDialogArea", channel);
		return comp;
	}
	
	/**
	 * 
	 * 
	 * @param parent 
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent){
		super.createButtonsForButtonBar(parent);

	}
	
	/**
	 * 
	 * 
	 * @param buttonId 
	 */
	@Override
	protected void buttonPressed(int buttonId){
		super.buttonPressed(buttonId);
	}
	
	/**
	 * 
	 */
	@Override
	protected void okPressed(){
		if ((elementcombo == null) || (elementcombo.getSelectionIndex() == -1)) {
			super.okPressed();
			return;
		}
		
		int indexE = elementcombo.getSelectionIndex();
		
		ElementImport ei= UMLFactory.eINSTANCE.createElementImport();
		
		ei.setImportedElement((PackageableElement)elementmodelList.get(indexE));
		ei.setAlias(((NamedElement)elementmodelList.get(indexE)).getName());
		currentPackage.getElementImports().add(ei);
		
		super.okPressed();
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public Object getValue(){
		return value;
	}
	
	/**
	 * 
	 */
	protected void runActionModel(){

		int index = modelcombo.getSelectionIndex();
		
		Package selectedPackage = packageList.get(index);
		List<Element> tmplist = new ArrayList<Element>();
		tmplist.addAll(selectedPackage.getOwnedElements());
		
		elementmodelList = new ArrayList<NamedElement>();
		for (int i=0 ; i < tmplist.size() ; i++) {
			Element current = tmplist.get(i);
			
			if (current instanceof PackageImport) {
				current = ((PackageImport) current).getImportedPackage();
				Iterator importIt = current.getOwnedElements().iterator();
				while (importIt.hasNext()) {
					Element imported = (Element) importIt.next();
					if (imported instanceof Classifier) {
						elementmodelList.add(imported);
					}
				}
			} else if ((current instanceof Classifier) || (current instanceof Package)) {
				elementmodelList.add(current);
			}
		}

		elementList = new String[elementmodelList.size()];
		for (int i=0 ; i < elementmodelList.size() ; i++) {
			elementList[i] = ((NamedElement) elementmodelList.get(i)).getName()+
			"   ["+((NamedElement)elementmodelList.get(i)).getQualifiedName()+"]";
		}

		elementcombo.setItems(elementList);
	}
}
